package model;

import java.util.ArrayList;
import java.util.Random;

public class Chromosome {
	
	double fitness;
	ArrayList<Integer> chromosome;
	double expVal;
	
	
	public void calcFitness()
	{
		Country country=Country.getIns();
		double pathCost=0;
		int citiesNum=chromosome.size();
		for (int i = 0; i < chromosome.size(); i++) {
			pathCost+=country.calcDistance(chromosome.get(i), chromosome.get((i+1)%citiesNum));
		}
		fitness=-pathCost;
	}
	
	public Chromosome(ArrayList<Integer> chromosome) {
		this.chromosome=chromosome;
		calcFitness();
	}

	public static Chromosome makeChromosome() {
		Country country=Country.getIns();
		int size=country.size();
		
		ArrayList<Integer> numToChoose=new ArrayList<Integer>();

		for (int i=0; i<size; ++i) {
			numToChoose.add(i);
		}

		ArrayList<Integer>chromosome=new ArrayList<Integer>();
		Random rand= new Random();
		for(int i=0;i<size;i++){
			int choosenNum=rand.nextInt(numToChoose.size());
			chromosome.add(numToChoose.get(choosenNum));
			numToChoose.remove(numToChoose.get(choosenNum));
		}
		return new Chromosome(chromosome);
	}

	public double getFitness() {
		return fitness;
	}


	public void setFitness(double fitness) {
		this.fitness = fitness;
	}


	public ArrayList<Integer> getChromosome() {
		return chromosome;
	}


	public void setChromosome(ArrayList<Integer> chromosome) {
		this.chromosome = chromosome;
	}


	public double getExpVal() {
		return expVal;
	}


	public void setExpVal(double expVal) {
		this.expVal = expVal;
	}
}
